package com.freak.wanandroid.model.classify;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.freak.httpmanager.BaseViewModel;
import com.freak.httpmanager.callback.ApiCallback;
import com.freak.httpmanager.callback.SubscriberCallBack;
import com.freak.wanandroid.bean.ClassifyBean;
import com.freak.wanandroid.bean.ClassifyTitleBean;
import com.freak.wanandroid.net.CommonLiveData;

import java.util.List;

public class ClassifyViewModel extends BaseViewModel<ClassifyRepository> {
    protected MutableLiveData<ClassifyBean> mGetClassify = new MutableLiveData<>();
    protected MutableLiveData<String> mGetClassifyError = new MutableLiveData<>();
    protected MutableLiveData<List<ClassifyTitleBean>> mGetClassifyTitle = new MutableLiveData<>();
    protected MutableLiveData<String> mGetClassifyTitleError = new MutableLiveData<>();
    protected MutableLiveData<CommonLiveData> mDoCollect = new MutableLiveData<>();
    protected MutableLiveData<CommonLiveData> mDoCancelCollect = new MutableLiveData<>();

    public ClassifyViewModel(@NonNull Application application) {
        super(application);
    }

    public void getClassify(String curPage, String cid) {
        mRepository.getClassify(curPage, cid, new SubscriberCallBack<>(new ApiCallback<ClassifyBean>() {
            @Override
            public void onSuccess(ClassifyBean model) {
                mGetClassify.postValue(model);
            }

            @Override
            public void onFailure(String msg) {
                mGetClassifyError.postValue(msg);
            }
        }));
    }

    public void getClassifyTitle() {
        mRepository.getClassifyTitle(new SubscriberCallBack<>(new ApiCallback<List<ClassifyTitleBean>>() {
            @Override
            public void onSuccess(List<ClassifyTitleBean> model) {
                mGetClassifyTitle.postValue(model);
            }

            @Override
            public void onFailure(String msg) {
                mGetClassifyTitleError.postValue(msg);
            }
        }));
    }

    public void doCancelCollect(int id) {
        mRepository.doCancelCollect(id, new SubscriberCallBack<>(new ApiCallback<String>() {
            @Override
            public void onSuccess(String model) {
                mDoCancelCollect.postValue(new CommonLiveData(CommonLiveData.SUCCESS, model));
            }

            @Override
            public void onFailure(String msg) {
                mDoCancelCollect.postValue(new CommonLiveData(CommonLiveData.ERROR, msg));
            }
        }));
    }

    public void doCollect(int id) {
        mRepository.doCollect(id, new SubscriberCallBack<>(new ApiCallback<String>() {
            @Override
            public void onSuccess(String model) {
                mDoCollect.postValue(new CommonLiveData(CommonLiveData.SUCCESS, model));
            }

            @Override
            public void onFailure(String msg) {
                mDoCollect.postValue(new CommonLiveData(CommonLiveData.ERROR, msg));
            }
        }));
    }
}
